xquery version "1.0";

module namespace activity="http://www.example.com/activity";
import module namespace addf="http://www.example.com/addf" at "xmldb:exist:///db/projdb/modules/addf.xqm";

declare namespace pm = "http://www.politicalmashup.nl";
declare namespace dc="http://purl.org/dc/elements/1.1/";

(: this module provides functions related to the degree of activity of individuals :)

(: gegeven een lijst van topics, filtert de nutteloze topics eruit ( = moties van orde + door voorzitter gesproken) :)
declare function activity:topics-to-take-into-account($topics) {
    $topics[data(@pm:title) != "MOTIE VAN ORDE" and data(@pm:title) != "MOTIES VAN ORDE"] (:data(pm:speech/@pm:role) != 'chair' and :)
};

(: geeft alle topics van een bepaalde initiator :)
declare function activity:topics-by-initiator($iandp_file, $initiator as xs:string) {
    $iandp_file/topics/topic[initiators[data(initiator) = $initiator]]
};

(: geeft alle topics waaraan een bepaalde persoon deelneemt :)
declare function activity:topics-by-participator($iandp_file, $participator as xs:string) {
    $iandp_file/topics/topic[participants[data(participant) = $participator]]
};

(: geeft alle topic-titels waarvan de input-persoon een initiatiefnemer is :)
declare function activity:topic-titels-from-initiator($iandp_file, $initiator as xs:string) {
    let $topics := activity:topics-by-initiator($iandp_file, $initiator)
    return activity:_create-topic-hierarchy($topics)
    
};

declare function activity:_create-topic-hierarchy($topics) {
    let $distinct-titles := distinct-values(data($topics/title))
    return document{
        element topics {
            for $title in $distinct-titles 
                let $topics-with-this-title := $topics[data(title) = $title]
                let $lowest-dates-with-this-title := 
                    for $topic in $topics-with-this-title
                        order by data($topic/@date)
                        return data($topic/@date)
                order by $lowest-dates-with-this-title[1]
                return element topic {
                    element title{$title},
                    for $topic in $topics-with-this-title
                        order by data($topic/@date)
                        return element id {
                            attribute date {data($topic/@date)},
                            data($topic/@id)
                        }
                }
        }}
};

(: geeft alle topic-titels waarvan de input-persoon een deelnemer is :)
declare function activity:topic-titels-from-participator($iandp_file, $participator as xs:string) {
    let $topics := activity:topics-by-participator($iandp_file, $participator)
    return activity:_create-topic-hierarchy($topics)
};

declare function activity:topic-titels-from-initiator-participator($iandp_file, $speaker as xs:string) {
    let $topics := activity:topics-by-initiator($iandp_file, $speaker) intersect activity:topics-by-participator($iandp_file, $speaker)
    return activity:_create-topic-hierarchy($topics)
};

declare function activity:number_of_initiated_topics($iandp_file, $speaker as xs:string) {
    let $topics := activity:topics-by-initiator($iandp_file, $speaker)
    return count(distinct-values(data($topics/title)))
};

declare function activity:number_of_participated_topics($iandp_file, $speaker as xs:string) {
    let $topics := activity:topics-by-participator($iandp_file, $speaker)
    return count(distinct-values(data($topics/title)))
};

declare function activity:number_of_participated_initiated_topics($iandp_file, $speaker as xs:string) {
    let $topics := activity:topics-by-initiator($iandp_file, $speaker) intersect activity:topics-by-participator($iandp_file, $speaker)
    return count(distinct-values(data($topics/title)))
};

declare function activity:averageNumberOfWords($doc, $speaker as xs:string) {
    let $topics := $doc/root/pm:proceedings/pm:topic[data(@pm:title) != "MOTIE VAN ORDE" and pm:speech[data(@pm:speaker) = $speaker]]
    let $content := string-join(data($topics/pm:speech[data(@pm:speaker) = $speaker]/pm:p), ' ')
    return if (count($topics) = 0) then 0 else addf:word-count($content) div count($topics)
};

(: geeft een xml document met voor iedere topic zijn initiatiefnemers en deelnemers :)
declare function activity:list-of-topic-participants-and-initiators($topics, $speakers)
{
    document {element result {
    for $topic in $topics 
        let $participants := for $participant in distinct-values(data($topic/pm:speech[@pm:role != 'chair']/@pm:speaker))
            return element participant {$participant}
        let $initiators-temp := for $speaker in $speakers
            where contains(addf:substring-before-if-contains(data($topic/@pm:title), ' tot '), $speaker)
            return element initiator { $speaker }
        let $initiators := if ($initiators-temp) then $initiators-temp else element initiator {data($participants[1])}
        where if ($participants and $initiators) then count($initiators | $participants) > 1 else false
        return element tupel {
            element title {data($topic/@pm:title)},
            $initiators,
            $participants
        }
        }}
};
